package com.aj.activity.dialog;

import java.util.Calendar;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.aj.activity.R;

public class ChooseDateDialog extends BaseDialog implements
		OnWheelChangedListener {
	private WheelView mYear;
	private WheelView mMouth;
	private WheelView mDay;

	private TextView cannel_button;
	private TextView sure_button;
	private String[] mYearData;
	private String[] mMothData;
	private String[] mDateData;

	private Calendar calendar;

	private int year;
	private int mouth;
	private int date;
	private IChooseDay chooseDay;

	public IChooseDay getChooseDay() {
		return chooseDay;
	}

	public void setChooseDay(IChooseDay chooseDay) {
		this.chooseDay = chooseDay;
	}

	public interface IChooseDay {
		public void chooseDaySuccess(int year, int mouth, int day);
	}

	public ChooseDateDialog(Context context) {
		super(context, R.layout.dialog_date);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_date);
		calendar = Calendar.getInstance();
		year = calendar.get(Calendar.YEAR);
		mouth = calendar.get(Calendar.MONTH) + 1;
		date = calendar.get(Calendar.DATE);

		mYear = (WheelView) findViewById(R.id.id_year);
		mMouth = (WheelView) findViewById(R.id.id_mouth);
		mDay = (WheelView) findViewById(R.id.id_date);
		ButtonClickEvent buttonClickEvent = new ButtonClickEvent();
		cannel_button = (TextView) findViewById(R.id.cannel_button);
		sure_button = (TextView) findViewById(R.id.sure_button);
		cannel_button.setOnClickListener(buttonClickEvent);
		sure_button.setOnClickListener(buttonClickEvent);
		initDatas();
		mYear.setViewAdapter(new ArrayWheelAdapter<String>(context, mYearData));
		mMouth.setViewAdapter(new ArrayWheelAdapter<String>(context, mMothData));
		mDay.setViewAdapter(new ArrayWheelAdapter<String>(context, mDateData));

		mYear.addChangingListener(this);
		mMouth.addChangingListener(this);
		mDay.addChangingListener(this);

		mYear.setVisibleItems(5);
		mMouth.setVisibleItems(5);
		mDay.setVisibleItems(5);
		updateYear();
		updateMoth();
		updateDate();

	}

	class ButtonClickEvent implements android.view.View.OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.cannel_button:
				dismiss();
				break;
			case R.id.sure_button:
				if (chooseDay != null) {
					chooseDay.chooseDaySuccess(mYear.getCurrentItem() + 1900,
							mMouth.getCurrentItem() + 1,
							mDay.getCurrentItem() + 1);
				}
				dismiss();
				break;
			}
		}
	}

	private void updateYear() {
		for (int i = 0; i < mYearData.length; i++) {
			if (year == Integer.parseInt(mYearData[i])) {
				mYear.setCurrentItem(i);
				break;
			}
		}
	}

	private void updateMoth() {
		for (int i = 0; i < mMothData.length; i++) {
			if (mouth == Integer.parseInt(mMothData[i])) {
				mMouth.setCurrentItem(i);
				initDateData(year, mouth);
				break;
			}
		}
	}

	private void updateDate() {
		for (int i = 0; i < mDateData.length; i++) {
			if (date == Integer.parseInt(mDateData[i])) {
				mDay.setCurrentItem(i);
				break;
			}
		}
	}

	private void initDatas() {
		initYearData();
		initMouthData();
		initDateData(calendar.get(Calendar.YEAR),
				calendar.get(Calendar.MONTH) + 1);
	}

	private void initYearData() {
		int yearCount = calendar.get(Calendar.YEAR) - 1899;
		mYearData = new String[yearCount];
		for (int i = 0; i < yearCount; i++) {
			mYearData[i] = (1900 + i) + "";
		}
	}

	private void initMouthData() {
		mMothData = new String[12];
		for (int i = 0; i < 12; i++) {
			mMothData[i] = (i + 1) + "";
		}
	}

	private void initDateData(int year, int mouth) {
		if (mouth == 2) {
			if (isLeapYear(year)) {
				mDateData = new String[29];
				for (int i = 0; i < 29; i++) {
					mDateData[i] = (i + 1) + "";
				}
			} else {
				mDateData = new String[28];
				for (int i = 0; i < 28; i++) {
					mDateData[i] = (i + 1) + "";
				}
			}
		} else {
			switch (mouth) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				mDateData = new String[31];
				for (int i = 0; i < 31; i++) {
					mDateData[i] = (i + 1) + "";
				}
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				mDateData = new String[30];
				for (int i = 0; i < 30; i++) {
					mDateData[i] = (i + 1) + "";
				}
				break;
			}
		}
	}

	private boolean isLeapYear(int year) {
		return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? true
				: false;
	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		if (wheel == mYear) {
			initDateData((mYear.getCurrentItem() + 1900),
					(mMouth.getCurrentItem() + 1));
			mMouth.setViewAdapter(new ArrayWheelAdapter<String>(context,
					mMothData));
			int current = mDay.getCurrentItem();
			mDay.setViewAdapter(new ArrayWheelAdapter<String>(context,
					mDateData));
			if (current >= mDateData.length) {
				mDay.setCurrentItem(mDateData.length - 1);
			}
		} else if (wheel == mMouth) {
			initDateData((mYear.getCurrentItem() + 1900),
					(mMouth.getCurrentItem() + 1));
			int current = mDay.getCurrentItem();
			mDay.setViewAdapter(new ArrayWheelAdapter<String>(context,
					mDateData));
			if (current >= mDateData.length) {
				mDay.setCurrentItem(mDateData.length - 1);
			}
		}
	}
}
